'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 资源补充消耗钻石功能
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var rewardService = require('../../../services/rewardService');
var roleService = require('../../../services/roleService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 资源补充消耗钻石
 */
Handler.prototype.receiveResources = function (msg, session, next) {
    var role_id = session.uid;
    var add_food = msg.add_food;
    var add_gold = msg.add_gold;
    var sub_diamond;

    add_food = parseInt(add_food);
    add_gold = parseInt(add_gold);

    if (add_food < 0 || add_gold < 0) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'build', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var build_model = player_manager.build;
                var now_gold = role_model.getGold();
                var now_food = role_model.getFood();
                var now_diamond = role_model.getDiamond();
                var max_gold_store = build_model.getMaxGoldStore();
                var max_food_store = build_model.getMaxFoodStore();

                // 资源是否超过上限
                if (add_food + now_food > max_food_store) {
                    add_food = max_food_store - now_food;
                }
                if (add_gold + now_gold > max_gold_store) {
                    add_gold = max_gold_store - now_gold;
                }

                // 扣除钻石
                sub_diamond = Math.ceil((add_gold + add_food) / 200);
                if (!roleService.subDiamond(player_manager, moduleType.MDL_OTHER, operationType.OPR_OTHER_DIRECT_ADD_RES, sub_diamond)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                roleService.addGoldLimit(player_manager, moduleType.MDL_OTHER, operationType.OPR_OTHER_DIRECT_ADD_RES, add_gold);
                roleService.addFoodLimit(player_manager, moduleType.MDL_OTHER, operationType.OPR_OTHER_DIRECT_ADD_RES, add_food);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    add_food: add_food,
                    add_gold: add_gold,
                    sub_diamond: sub_diamond
                }
            });
        }
    );
};
